package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.DateUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowLogVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.RuleActionAuditQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.RuleActionAuditVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowRuleActionAuditLogRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowRuleActionAuditLog;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowRuleActionAuditLogExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowRuleActionAuditLogDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lilh
 * @date 2020/11/4 16:55
 */
@Repository
public class FlowRuleActionAuditLogRepositoryImpl implements FlowRuleActionAuditLogRepository {

    private static final String ORDER_BY_ASC = "ASC";

    @Autowired
    private FlowRuleActionAuditLogDAO flowRuleActionAuditLogDAO;


    @Override
    public PageInfo<RuleActionAuditVo> listRuleActionAudit(RuleActionAuditQueryVo vo) {
        FlowRuleActionAuditLogExample example = new FlowRuleActionAuditLogExample();
        FlowRuleActionAuditLogExample.Criteria criteria = example.createCriteria();
        if (CollectionUtils.isNotEmpty(vo.getOperator())) {
            criteria.andOperatorIn(vo.getOperator());
        }
        if (Objects.nonNull(vo.getAppId())) {
            criteria.andAppIdEqualTo(vo.getAppId());
        }
        if (Objects.nonNull(vo.getBusiId())) {
            criteria.andBusiIdEqualTo(vo.getBusiId());
        }
        if (StringUtils.isNotEmpty(vo.getModelName())) {
            criteria.andModelNameLike(appendLikeCondition(vo.getModelName()));
        }
        if (StringUtils.isNotEmpty(vo.getModelId())) {
            criteria.andModelIdLike(appendLikeCondition(vo.getModelId()));
        }
        if (CollectionUtils.isNotEmpty(vo.getModelStatus())) {
            criteria.andModelStatusIn(vo.getModelStatus());
        }
        if (StringUtils.isNotEmpty(vo.getTimeStart()) && StringUtils.isNotEmpty(vo.getTimeEnd())) {
            Date startDate = DateUtils.string2OldDate(vo.getTimeStart());
            Date endDate = DateUtils.string2OldDate(vo.getTimeEnd());
            criteria.andOperatingTimeBetween(startDate, endDate);
        }
        String operatingTime = "operating_time ";
        String orderBy = "desc";
        if (StringUtils.isNotEmpty(vo.getOperatingTimeOrderBy())) {
            if (ORDER_BY_ASC.equalsIgnoreCase(vo.getOperatingTimeOrderBy())) {
                orderBy = ORDER_BY_ASC;
            }
        }
        example.setOrderByClause(operatingTime + orderBy);
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<FlowRuleActionAuditLog> list = flowRuleActionAuditLogDAO.selectByExample(example);
        PageInfo<FlowRuleActionAuditLog> pageInfo = new PageInfo<>(list);
        List<RuleActionAuditVo> results = new ArrayList<>();
        for (FlowRuleActionAuditLog log : list) {
            RuleActionAuditVo reslut = new RuleActionAuditVo();
            BeanUtils.copyProperties(log, reslut);
            reslut.setOperatingTime(DateUtils.dateToString(log.getOperatingTime()));
            results.add(reslut);
        }
        PageInfo<RuleActionAuditVo> pageResult = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setList(results);
        return pageResult;
    }

    private String appendLikeCondition(String fieldName) {
        StringBuilder sb = new StringBuilder();
        sb.append("%").append(fieldName).append("%");
        return sb.toString();
    }


    @Override
    public ResponseResult list(FlowLogVo vo) {
        FlowRuleActionAuditLogExample example = new FlowRuleActionAuditLogExample();
        FlowRuleActionAuditLogExample.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(vo.getBeginTime())&Objects.nonNull(vo.getEndTime())) {
            criteria.andCreateTimeBetween(vo.getBeginTime(),vo.getEndTime());
        }
        if (StringUtils.isNotEmpty(vo.getKeyWord())) {
            criteria.andModelNameLike("%"+vo.getKeyWord()+"%");
        }
        if (StringUtils.isNotEmpty(vo.getAction())) {
            criteria.andActionEqualTo(vo.getAction());
        }
        if (Objects.nonNull(vo.getTenantId())) {
            criteria.andTenantIdEqualTo(vo.getTenantId());
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<FlowRuleActionAuditLog> list = flowRuleActionAuditLogDAO.selectByExample(example);
        PageInfo<FlowRuleActionAuditLog> pageInfo = new PageInfo<>(list);
        ResponseResult responseResult = ResponseResult.success(list);
        responseResult.setTotalCount((int) pageInfo.getTotal());
        responseResult.setPageNum(vo.getPageNum());
        responseResult.setPageSize(vo.getPageSize());
        return responseResult;
    }

    @Override
    public ResponseResult detail(Integer id) {
        FlowRuleActionAuditLog flowRuleActionAuditLog = flowRuleActionAuditLogDAO.selectByPrimaryKey(id);
        return ResponseResult.success(flowRuleActionAuditLog);
    }
}
